using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Tryit.Models;
using Tryit.Views;

namespace Tryit.ViewModels
{
    public class MaterialEditViewModel : BindableBase
    {
        private readonly Material _material;
        private readonly Window _window;
        private MaterialElement _selectedElement;

        public Material Material
        {
            get => _material;
        }

        public MaterialElement SelectedElement
        {
            get => _selectedElement;
            set => SetProperty(ref _selectedElement, value);
        }

        private DelegateCommand _addElementCommand;
        public DelegateCommand AddElementCommand => _addElementCommand ??= new DelegateCommand(ExecuteAddElement);

        private DelegateCommand<MaterialElement> _deleteElementCommand;
        public DelegateCommand<MaterialElement> DeleteElementCommand => _deleteElementCommand ??= new DelegateCommand<MaterialElement>(ExecuteDeleteElement);

        private DelegateCommand _confirmCommand;
        public DelegateCommand ConfirmCommand => _confirmCommand ??= new DelegateCommand(ExecuteConfirm);

        private DelegateCommand _cancelCommand;
        public DelegateCommand CancelCommand => _cancelCommand ??= new DelegateCommand(ExecuteCancel);

        public MaterialEditViewModel(Material material, Window window)
        {
            _material = material;
            _window = window;
            _selectedElement = new MaterialElement();
        }

        private void ExecuteAddElement()
        {
            var element = new MaterialElement("新元素", 0.0);
            Material.Elements.Add(element);
            SelectedElement = element;
        }

        private void ExecuteDeleteElement(MaterialElement element)
        {
            if (element == null) return;

            var result = MessageBox.Show($"确定要删除元素 '{element.ElementName}' 吗？", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                Material.Elements.Remove(element);
                if (SelectedElement == element)
                {
                    SelectedElement = Material.Elements.FirstOrDefault();
                }
            }
        }

        private void ExecuteConfirm()
        {
            if (string.IsNullOrWhiteSpace(Material.Name))
            {
                MessageBox.Show("请输入材料名称", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (Material.Elements.Count == 0)
            {
                MessageBox.Show("请至少添加一个化学元素", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            // 检查是否有重复的元素名称
            var duplicateElements = Material.Elements
                .GroupBy(e => e.ElementName)
                .Where(g => g.Count() > 1)
                .Select(g => g.Key)
                .ToList();

            if (duplicateElements.Any())
            {
                MessageBox.Show($"存在重复的元素名称：{string.Join(", ", duplicateElements)}", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            _window.DialogResult = true;
            _window.Close();
        }

        private void ExecuteCancel()
        {
            _window.DialogResult = false;
            _window.Close();
        }
    }
} 